package Controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.CategoryModel;
import POJO.UserPOJO;

/**
 * Servlet implementation class Category
 */
@WebServlet("/category")
public class Category extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Category() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserPOJO user  =  (UserPOJO)session.getAttribute("user");

		if(user != null)
		{
			//kiem tra phan quyen
			if(user.getType().equals("Merchandise") || user.getType().equals("System Admin")  )
			{
				request.setAttribute("do", "true");
			}
			
			request.setAttribute("menu", session.getAttribute("menu"));
			request.setAttribute("user", user);
			request.setAttribute("view", "Categories");
			request.setAttribute("url", "category");
			
			if(request.getParameter("add")!= null)
			{
				request.setAttribute("action", "Add");
				RequestDispatcher rd = request.getRequestDispatcher("View/Add.jsp");
				rd.forward(request, response);
			}
			else if(request.getParameter("id")!= null)
			{
				request.setAttribute("action", "Add");
				request.setAttribute("id",request.getParameter("id"));
				RequestDispatcher rd = request.getRequestDispatcher("View/Add.jsp");
				rd.forward(request, response);
			}
			else
			{
				request.setAttribute("action", "View");
				RequestDispatcher rd = request.getRequestDispatcher("View/View.jsp");
				rd.forward(request, response);
			}
		}
		else
		{
			response.sendRedirect("login");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String choose=(String)request.getParameter("do");
		switch(choose)
		{
			case "delete":{DeleteCategory(request,response);break;}
			case "Add":{InsertCategory(request,response);break;}
			case "Edit":{UpdateCategory(request,response);break;}
			case "Search":{response.sendRedirect("category?key=" + request.getParameter("key"));break;}
		}
	}
	private void InsertCategory (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String name=request.getParameter("txtcategory");
		int kq=CategoryModel.InsertCategory(name);
		HttpSession session = request.getSession();
		if(kq==1)
		{
			session.setAttribute("doSuscess", "Category added sucessfully");
			//writer.println("Insert suscess");
		}
		else
		{
			session.setAttribute("doError", "Category added Error");
			//writer.println("Insert fail");
		}
		response.sendRedirect("category");
	}
	private void UpdateCategory (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String name=request.getParameter("txtcategory");
		String id=request.getParameter("idedit");
		int kq=CategoryModel.UpdateCategory(name, id);
		HttpSession session = request.getSession();
		if(kq==1)
		{
			session.setAttribute("doSuscess", "Category updated sucessfully");
			//writer.println("Insert suscess");
		}
		else
		{
			session.setAttribute("doError", "Category updated Error");
			//writer.println("Insert fail");
		}
		response.sendRedirect("category");
		
	}
	private void DeleteCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		int id=Integer.parseInt(request.getParameter("id"));
		int kq=CategoryModel.DeleteCategory(id);
		HttpSession session = request.getSession();
		if(kq==1)
		{
			session.setAttribute("doSuscess", "Category deleted sucessfully");
			//writer.println("Insert suscess");
		}
		else
		{
			session.setAttribute("doError", "Category deleted Error");
			//writer.println("Insert fail");
		}
		response.sendRedirect("category");
	
	}
}
